package jm.algorithm.recursion;

/**
 * @Description 尾递归
 * @date 2022/5/18 10:16
 */
public class TailRecursion {

    public static void main(String[] args) {
        System.out.println(factorial(10));
    }

    private static int factorial(int n){
        if (n <= 1) return n;
        return n * factorial(n - 1);
    }

    private static int factorialTail(int n){
        return factorialTail(n, 1);
    }

    private static int factorialTail(int n, int result){
        if (n <= 1) return result;
        return factorialTail(n - 1, n * result);
    }

    private static int fib(int n){
        if (n <= 2) return 1;
        return fib(n - 1) + fib(n - 1);
    }


    private static int fibTail(int n){
        return fibTail(n, 1, 1);
    }

    private static int fibTail(int n, int first, int second){
        if (n <= 2) return first;
        return fibTail(n-1, second,first + second);
    }


}
